package com.tianbing.adlottery.controller.storeadmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.Site;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;
import com.tianbing.adlottery.util.UtilDataGroup;

public class UpdateMyStorePositionCategoryController extends SimpleFormController {
	static Logger logger = Logger.getLogger(UpdateMyStorePositionCategoryController.class.getName());

	private ICategoryDao categoryDao;

	private IUserBusiness userBusiness;

	private ISiteBusiness siteBusiness;

	private SkinUtils skinUtils;

	private ISiteDao siteDao;

	// IPartyService partyService;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		Site site = userBusiness.findUserManagedStore(auth.getUserSysId());
		if (site == null) {
			site = new Site();
		}
		fb.setSite(site);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());

		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);

		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", IConstants.CategoryID.AREA_TOP.getId());
		siteBusiness.setStorePositionCategory(auth.getManagedStoreSysId(), sgid);

		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", IConstants.CategoryID.AREA_TOP.getId());

		// get category by gid
		Category siteCategory = categoryDao.findCategoryByCategoryId(sgid);
		List primarySiteCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(sgid);
		// get child categories of gid
		List<Category> childCategoryList = categoryDao.getListChildCategoryBy(sgid, 0, 100, null);
		List siteChildCategoryListGroups = UtilDataGroup.makeGroups(childCategoryList, 0, 5);
		model.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
		model.put("siteChildCategoryListGroups", siteChildCategoryListGroups);

		// get store position
		UserSession auth = skinUtils.getUserAuthorization(request, null);

		List categoryTrails = siteBusiness.getSiteCategoryTrails(auth.getManagedStoreSysId(),
				IConstants.CategoryID.AREA_TOP.getId());
		model.put("storeAreaCategoryTrails", categoryTrails);

		List categoryBizTrails = siteBusiness.getSiteCategoryTrails(auth.getManagedStoreSysId(),
				IConstants.CategoryID.BUSINESS_TOP.getId());
		model.put("storeBusinessCategoryTrails", categoryBizTrails);

		// prepare output data map for view
		model.put("siteCategory", siteCategory);

		return model;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		UpdateMyStorePositionCategoryController.logger = logger;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}
}
